<?php
/**
 * 图片画册
 * AUTHOR  : zlf
 * DateTime: 2022/06/18 09:00
 */
namespace App\Api\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Http\Controllers\Api\BaseController; 
use App\Api\Services\PictureEditingService;
use App\Api\Services\TextImageService;
use App\Api\Services\TextImageTemplatesService;
use App\Api\Services\ImageLabelService;
use App\Api\Models\ImageLabelModel;
use App\Api\Models\ContentTagsModel;

class PictureEditingController extends BaseController
{
 /**
   * 新建图片画册
   * 
   * @param album_cover_img  画册封面
   * @param p_a_name         画册名称
   * @param mobile           联系电话
   * @param background_music 背景音乐
   * @param label_data       标签数据包
   * @param Album_imgs       画册内容图片(多图逗号分隔)
   * @param label_id         标签id
   */
   public function addPictureAlbum(Request $request){    
      //参数验证
      $validator = Validator::make($data = $request->all(), [
          'p_a_name' =>'required',
          'mobile'   => 'required',
         /*  'mobile'   => 'regex:/^1[3456789][0-9]{9}$/' */
      ],[
          'p_a_name.required' => '请填写画册名称',         
          'mobile.required'   => '请填写联系电话',
          /* 'mobile.regex'      => '手机号格式错误', */
          
         
      ]);
      //参数错误响应
      if ($info = $validator->errors()->first()) {         
          return response()->json([
            'code' => 1,
            'msg' => $info,
            'data' => ''
        ]);
      }
      
       /* $json = '[{"label_id":7,"label_name":"掌上网络7777","Album_imgs":"77.png,77.png"},{"label_id":8,"label_name":"掌上网络888","Album_imgs":"888.png,888.png"}]';
           
      $data['label_data'] = json_decode($json,true);  */

      $imgs_data = SElf::handleImgsData($request);// 处理图片画册 封面、音乐 函数
      
      if($imgs_data['code'] === 1){
          
            return response()->json($imgs_data); 
       
      }elseif ($imgs_data['code'] === 0){
          
            $imgs_data = $imgs_data['data'];
          
      }
      
      $data['create_time'] = time();
      $data['update_time'] = time();

      $data = array_merge($data,$imgs_data);
      
      $data['user_id']     = $this->getUserId();
      $data['main_id']     = $this->getMainUserId();
      
     if(isset($data['label_data'])){
         
         if(!is_array($data['label_data'])){
      
          $data['label_data'] = json_decode($data['label_data'],true);
          
         }
         
         $lData['label_data'] = $data['label_data'];
         unset($data['label_data']);
      }else{
          $lData['label_data']='';
      } 

      $id = PictureEditingService::addPictureAlbum($data);

        if($id){
            
            if(!empty($lData['label_data'])){
                
                foreach ($lData['label_data'] as $k=>$v){
                   
                    $updData['pa_id']      = $id;
                    $updData['label_name'] = $v['label_name'];
                    $updData['Album_imgs'] = $v['Album_imgs'];
                    
                    $lCondition['id']      =   $v['label_id'];
                    $lCondition['user_id'] =   $this->getUserId();
                    
                    $iInfo = ImageLabelService::getImageLabelInfo($lCondition);
                    
                    if(!empty($iInfo['Album_imgs'])){
                        
                        $iInfo['Album_imgs'] = explode(',',$iInfo['Album_imgs']);
                        $v['Album_imgs']     = explode(',',$v['Album_imgs']);
                        
                        $del_img_arr = array_merge(array_diff($iInfo['Album_imgs'],$v['Album_imgs']),array_diff($v['Album_imgs'],$iInfo['Album_imgs']));
                        
                        if(!empty($del_img_arr) && is_array($del_img_arr)){
                        
                            foreach ($del_img_arr as $k=>$v){
                        
                                if(empty($v)){
                                     
                                    if(is_file(public_path($v))){
                                         
                                        unlink(public_path($v));
                                    }
                                }
                            }
                        }  
                    }
                    ImageLabelService::updImageLabel($lCondition, $updData);
                }
                unset($lData['label_data']);
            }
            
            unset($updData);
            
            $this->log('图片画册,添加成功', $id); 
            
            return response()->json(['code' => 0,'msg'  => '添加成功','data' => '']);
        }
        
        return response()->json(['code' => 1,'msg'  => '添加失败','data' => '']);
      
   }
   
  
   /**
    * 删除图片画册
    * @param pa_ids 图片画册id(多个用逗号分开)
    */
   public function delPictureAlbum(Request $request){
       
       $validator = Validator::make(
           $data  = $request->all(), [
               'pa_ids' =>'required'
           ],[
               'pa_ids.required' => '请选择图片画册！'
           ]); 
       //参数错误响应
       if ($info = $validator->errors()->first()) {
           return response()->json(['code' => 1,'msg' => $info,'data' => '']);
       }
       
       $condition['user_id'] = $this->getUserId();
       $pa_id_arr            = explode(',', $data['pa_ids']);
       
       $pa_info = PictureEditingService::getPictureAlbumInfos($condition,$pa_id_arr);      
       $bool    = PictureEditingService::delPictureAlbum($condition, $pa_id_arr);     
       if($bool){
           if(!empty($pa_info)){
               
              foreach ($pa_info as $pkey=>$pval){
                  
                   if(!empty($pval['album_cover_img'])){//删除封面图片
                       
                       if(is_file(public_path($pval['album_cover_img']))){
                           
                       unlink(public_path($pval['album_cover_img']));
                       
                       }
                   }
                    
                   if(!empty($pval['background_music'])){//删除背景音乐
                       
                       if(is_file(public_path($pval['background_music']))){
                           
                       unlink(public_path($pval['background_music']));
                       }
                   }
     
               }
               
               $iList = ImageLabelModel::select('Album_imgs')->where($condition)->whereIn('pa_id',$pa_id_arr)->get();
                
               if(!empty($iList)){
                   
                   ImageLabelModel::select('Album_imgs')->where($condition)->whereIn('pa_id',$pa_id_arr)->delete();
                   
                   foreach ($iList as $ik=>$iv){
                        
                       if(!empty($iv['Album_imgs'])){//删除上传图片
               
                           $iv['Album_imgs']= explode(',', $iv['Album_imgs']);
               
                           foreach ($iv['Album_imgs'] as $key => $val){
               
                               if(is_file(public_path($val))){
               
                                   unlink(public_path($val));
                               }
                           }
                       }
                   }
               }
           }
           $this->log('图片画册,删除成功', $data);
           return response()->json(['code' => 0,'msg'  => '删除成功','data' => '']);
                 
       }
       
       return response()->json(['code' => 1,'msg'  => '删除失败','data' => '']);
   }
   /**
    * 删除首页画册
    * @param img_ids  图片画册id(多个用逗号分隔)
    * @param text_ids 文字画册id(多个用逗号分隔)
    */
   public function delIndexAlbum(Request $request){
       
       if(!empty($request->input('img_ids'))){//首页删除图片画册

           $validator = Validator::make(
               $data  = $request->all(), [
                   'img_ids' =>'required'
               ],[
                   'img_ids.required' => '请选择图片画册！'
               ]);
           //参数错误响应
           if ($info = $validator->errors()->first()) {
               return response()->json(['code' => 1,'msg' => $info,'data' => '']);
           }
            
           $condition['user_id'] = $this->getUserId();
           $pa_id_arr            = explode(',', $data['img_ids']);
          
           $pa_info = PictureEditingService::getPictureAlbumInfos($condition,$pa_id_arr);
          
           $bool    = PictureEditingService::delPictureAlbum($condition, $pa_id_arr);
           
           if($bool){
              if(!empty($pa_info)){  
               foreach ($pa_info as $pkey=>$pval){
           
                   if(!empty($pval['album_cover_img'])){//删除封面图片
                        
                       if(is_file(public_path($pval['album_cover_img']))){
                            
                           unlink(public_path($pval['album_cover_img']));
                            
                       }
                   }
           
                   if(!empty($pval['background_music'])){//删除背景音乐
                        
                       if(is_file(public_path($pval['background_music']))){
                            
                           unlink(public_path($pval['background_music']));
                       }
                   }
               }
               
               $iList = ImageLabelModel::select('Album_imgs')->where($condition)->whereIn('pa_id',$pa_id_arr)->get();
               
               if(!empty($iList)){
                   
                   ImageLabelModel::select('Album_imgs')->where($condition)->whereIn('pa_id',$pa_id_arr)->delete();
                   
                   foreach ($iList as $ik=>$iv){
               
                       if(!empty($iv['Album_imgs'])){//删除上传图片
                            
                           $iv['Album_imgs']= explode(',', $iv['Album_imgs']);
                            
                           foreach ($iv['Album_imgs'] as $key => $val){
                                
                               if(is_file(public_path($val))){
                                    
                                   unlink(public_path($val));
                               }
                           }
                       }
                   }
                }
              }
                
               $this->log('图片画册,删除成功', $data);
               
                
           }else{
            
               return response()->json(['code' => 1,'msg'  => '删除失败','data' => '']);
            }
               
       }
       if(!empty($request->input('text_ids'))){//首页删除内容画册          
           //参数验证
           $validator = Validator::make($data = $request->all(), [
               'text_ids'          => 'required'
           ],
               [
                   'text_ids.required' => '缺少参数，文字画册id'
               ]);
           //参数错误响应
           if ($info = $validator->errors()->first()) {
           
               return response()->json(['code' => 1,'msg'  => $info,'data' => '']);
           }
           
           $condition['user_id'] = $this->getUserId();
           
           $pa_id_arr = explode(',',$data['text_ids']);
           
           $res = TextImageService::getTextImageInfos($condition, $pa_id_arr);
           
           if(empty($res)){
                
               return response()->json(['code' => 1,'msg'  => '要删除的文字画册，没找到!','data' => '']);
                
           }
           
           $bool = TextImageService::delTextImage($condition,$pa_id_arr);
           
           if($bool){
           
               foreach ($res as $tk=>$tv){
           
                   if(is_file(public_path($tv['album_cover_img']))){
           
                       unlink(public_path($tv['album_cover_img']));//删除服务器中的封面
                   }
           
                   if(is_file(public_path($tv['background_music']))){
           
                       unlink(public_path($tv['background_music']));//删除服务器中的背景音乐
                   }
               }
           
               $t_res = TextImageTemplatesService::getTextImageTemplatesInfos($condition, $pa_id_arr);
           
               if(!empty($t_res)){
                   //删除画册对应的内容模板
                   TextImageTemplatesService::delContentTempLates($condition, $pa_id_arr);
                    
                   foreach ($t_res as $tik=>$tiv){
           
                       if(!empty($tiv['temp_img_video_01'])){
           
                           $tiv['temp_img_video_01'] = explode(",",$tiv['temp_img_video_01']);
           
                           foreach ($tiv['temp_img_video_01'] as $iv1k =>$iv1v){//删除素材1的文件
           
                               if(is_file(public_path($iv1v))){
           
                                   unlink(public_path($iv1v));//删除服务器中素材1的文件
                               }
                           }
                       }
                       if(!empty($tiv['temp_img_video_02'])){//删除素材2的文件
           
                           $tiv['temp_img_video_02'] = explode(",",$tiv['temp_img_video_02']);
           
                           foreach ($tiv['temp_img_video_02'] as $iv2k =>$iv2v){
           
                               if(is_file(public_path($iv2v))){
           
                                   unlink(public_path($iv2v));//删除服务器中素材2的文件
                               }
                           }
                       }
                   }
           
               }
               
            ContentTagsModel::where($condition)->whereIn('pa_id',$pa_id_arr)->delete();
           
               $this->log('文字图片画册,删除成功', $bool);
              
                
           }else{
               
               return response()->json(['code'=>1,'msg'=>'删除失败','data'=>'']);
           } 
       }  
       
               return response()->json(['code'=>0,'msg'=>'删除成功','data'=>'']);
   }
   /**
    * 删除图片画册上传的图片
    * 
    * @param del_img_paths  要删除的图片路径
    * @param label_id       标签id 
    */  
   public function delAlbumImgs(Request $request){
      
        $validator = Validator::make(
            $data  = $request->all(), [
          'del_img_paths' =>'required'
          ],[
          'del_img_paths.required' => '请选择要删除的图片'        
          ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
          
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => ''
            ]);
        }       

        $img_paths=explode(',', $data['del_img_paths']);

        if(!empty($data['label_id'])){
            
            $condition['id'] = $data['label_id'];
            $condition['user_id'] = $this->getUserId();
            $info = ImageLabelService::getImageLabelInfo($condition);  
            
            $Album_imgs=explode(',', $info['Album_imgs']);
            
            $arr = array_merge(array_diff($Album_imgs,$img_paths),array_diff($img_paths,$Album_imgs));

            $update_data['Album_imgs']=implode(',', $arr);
            
            $update_data['update_time']=time();
            
            $bool=ImageLabelService::updImageLabel($condition,$update_data);
            
            if($bool){
                
                if(!in_array($Album_imgs[0], $arr) && $info['album_cover_img']==$Album_imgs[0]){
                
                    $p_data['album_cover_img'] = $arr[0];
                    $PCondition['id']          = $info['pa_id'];                   
                    PictureEditingService::updateAlbumImgs($PCondition, $p_data);
                }  
                
              $this->log('删除图片画册图片', $bool);  
            }
            
        }
        
        if(is_array($img_paths)){
        
            foreach ($img_paths as $key=>$val){
                
                if(is_file(public_path($val))){
                    
                    unlink(public_path($val));
                
                }
            }
        
        }else{
            if(is_file(public_path($data['del_img_paths']))){
                
                unlink(public_path($data['del_img_paths']));
            }
        }
        return response()->json(['code' => 0,'msg' => '删除成功','data' => '']);        
   }
   
   /**
    * 修改图片画册
    * 
    * @param album_cover_img  画册封面
    * @param p_a_name         画册名称
    * @param mobile           联系电话
    * @param background_music 背景音乐
    * @param pa_id            图片画册id  
    * @param label_data       标签数据包
    * @param label_id         标签
    * @param Album_imgs       画册内容图片(多图逗号分隔)
    */
   public function updatePictureAlbum(Request $request){
       
       $validator = Validator::make(
           $data  = $request->all(), [
               'pa_id' =>'required'
           ],[
               'pa_id.required' => '画册的id,不能为空!'
           ]);
       //参数错误响应
       if ($info = $validator->errors()->first()) {
       
           return response()->json(['code' => 1,'msg' => $info,'data' => '']);
       }
       /* $json='[{"label_id":3,"label_name":"掌上网络038","Album_imgs":"38.png,38.png"},{"label_id":4,"label_name":"掌上网络044","Album_imgs":"44.png,44.png"}]';
        
       $data['label_data']=json_decode($json,true); */
      
       $condition['id']=$data['pa_id'];
       $condition['user_id']=$this->getUserId();
       
       unset($data['pa_id']);
       
       $info=PictureEditingService::getPictureAlbumInfo($condition);
       
       if(empty($info)){
           
           return response()->json(['code' => 1,'msg' => '修改的图片画册，没有找到！','data' => '']);
       }
       
       $imgs_data=SElf::handleImgsData($request);
      
      if($imgs_data['code'] === 1){
          
       return response()->json($imgs_data); 
       
      }elseif ($imgs_data['code'] === 0){
          
          $imgs_data = $imgs_data['data'];
      }
      
       $data['update_time'] = time();
       
       $data=array_merge($data,$imgs_data);
       
       if(!empty($data['p_a_name']))
       $data['p_a_name']=$data['p_a_name'];
       
       if(!empty($data['mobile'])){
           
           $data['mobile']=$data['mobile'];
       }

       if(isset($data['label_data'])){
            
           if(!is_array($data['label_data'])){
       
               $data['label_data'] = json_decode($data['label_data'],true);
       
           }
            
           $lData['label_data'] = $data['label_data'];
           unset($data['label_data']);
       }else{
           $lData['label_data']='';
       }
        
       $bool = PictureEditingService::updateAlbumImgs($condition, $data);
 
       if($bool){
           
           if(empty($data['album_cover_img'])){
               
               if(is_file(public_path($info['album_cover_img']))){
                   
                    unlink(public_path($info['album_cover_img'])); 
               }
           }
           
           if(empty($data['background_music'])){
               
               if(is_file(public_path($info['background_music']))){ 
                   
                unlink(public_path($info['background_music']));
               }
           }
           
           if(!empty($lData['label_data'])){
           
               foreach ($lData['label_data'] as $k=>$v){
                   
                   $lCondition['id']  = $v['label_id'];

                   if(isset($v['Album_imgs'])){
                        
                       $updData['Album_imgs'] = $v['Album_imgs'];
                   }
                   if(isset($v['label_name'])){
                   
                       $updData['label_name'] = $v['label_name'];
                   }
                   $info = ImageLabelService::getImageLabelInfo($lCondition);
                   
                   $bol  = ImageLabelService::updImageLabel($lCondition, $updData);
                   
                   if($bol){
                       
                       if(!empty($info)){
                           
                        $info['Album_imgs'] = explode(',',$info['Album_imgs']);
                        $v['Album_imgs']    = explode(',',$v['Album_imgs']);
                        
                        $del_img_arr = array_merge(array_diff($info['Album_imgs'],$v['Album_imgs']),array_diff($v['Album_imgs'],$info['Album_imgs']));
                        
                        if(!empty($del_img_arr) && is_array($del_img_arr)){
                          
                            foreach ($del_img_arr as $k=>$v){
                                
                                if(empty($v)){
                                     
                                    if(is_file(public_path($v))){
                                         
                                        unlink(public_path($v));
                                    }
                                }  
                            }
                        }
                       
                    }
                       
                   }
               }
               unset($lData);
           }  
           unset($updData);
           $this->log('图片画册,编辑成功', $bool);
           return response()->json(['code'=>0,'msg'=>'编辑成功','data'=>'']);
       }
           return response()->json(['code'=>1,'msg'=>'编辑失败','data'=>'']);
     
       
   }
   
   /**
    * 图片画册列表
    * @param curr_page  页码
    */
   public function getPictureAlbumList(Request $request){
        
       $condition['pa.user_id']=$this->getUserId();
       $currentPage = !empty($request->input('curr_page'))?$request->input('curr_page'):1;
       $pageSize = !empty($request->input('page_size'))?$request->input('page_size'):20;
       $list = PictureEditingService::getPictureAlbumList($condition, $currentPage,$pageSize);

       if(!empty($list['data'])){
           foreach ($list['data'] as $k=>$val){              
               $iCondition['pa_id'] = $val['id'];
               $iList=ImageLabelService::getImageLabelLists($iCondition);
               if(!empty($iList)){
                   foreach ($iList as $ik=>$iv){
                       if(!empty($iv['Album_imgs'])){
                           $list['data'][$k]['label_data'][$ik]['id'] =$iv['id'];
                           $list['data'][$k]['label_data'][$ik]['pa_id'] =$iv['pa_id'];
                           $list['data'][$k]['label_data'][$ik]['is_top'] =$iv['is_top'];
                           $list['data'][$k]['label_data'][$ik]['sort_number'] =$iv['sort_number'];
                           $list['data'][$k]['label_data'][$ik]['label_name'] =$iv['label_name'];
                           $list['data'][$k]['label_data'][$ik]['Album_imgs'] =explode(',',$iv['Album_imgs']);
                           $list['data'][$k]['label_data'][$ik]['create_time']=date('Y-m-d H:i:s',$iv['create_time']);
                           $list['data'][$k]['label_data'][$ik]['update_time']=date('Y-m-d H:i:s',$iv['update_time']);
                           
                       }
                   }
               }else {
                   
                   $list['data'][$k]['label_data']=[];
               }
           }
           
       }
       return response()->json(['code'=>0,'msg'=>'查询成功','data'=>$list]);
   }
   

   
   
   /**
    * 画册首页-图片画册和文字画册
    */
   public function getImagesTextAlbumList(Request $request){
   
       $condition['pa.user_id'] = $this->getUserId();
        
       $currentPage = !empty($request->input('curr_page'))?$request->input('curr_page'):1;
       $pageSize = !empty($request->input('page_size'))?$request->input('page_size'):20;
       $p_list = PictureEditingService::getPictureAlbumList($condition, $currentPage,$pageSize);
   
       unset($condition['pa.user_id']);
        
       $condition['ti.user_id'] = $this->getUserId();
       $t_list = TextImageService::getTextImageList($condition, $currentPage,$pageSize);
        
       if(!empty($p_list['data']) && !empty($t_list['data'])){//图片画册不为空，文字画册不为空，返回文字画册和文字画册合并值
            
           $data = array_merge($p_list['data'],$t_list['data']);
   
           $time_arr = [];
           // 取得列的列表
           foreach ($data as $key => $val) {
   
               $time_arr[$key]      = $val['create_time'];
   
           }
           array_multisort($time_arr, SORT_DESC, $data);
   
           foreach ($data as $tkey =>$tval){
   
               $data[$tkey]['update_time']  = date('Y-m-d H:i:s',$tval['update_time']);
               $data[$tkey]['create_time'] = date('Y-m-d H:i:s',$tval['create_time']);
   
           }
   
           if(empty($p_list['next_page_url']) && !empty($t_list['next_page_url'])){
   
               $t_list['data'] = $data;
   
               $p_list         = $t_list;
           }else{
   
               $p_list['data'] = $data;
   
           }
   
           $p_list['total']=$t_list['total']+$p_list['total'];
   
           return response()->json(['code'=>0,'msg'=>'查询成功','data'=>$p_list]);
            
       }elseif (!empty($p_list['data']) && empty($t_list['data'])){//图片画册不为空，文字画册为空，返回图片画册值
            
           return response()->json(['code'=>0,'msg'=>'查询成功','data'=>$p_list]);
            
       }elseif(empty($p_list['data']) && !empty($t_list['data'])){//图片画册为空，文字画册不为空，返回文字画册值
            
           return response()->json(['code'=>0,'msg'=>'查询成功','data'=>$t_list]);
            
       }else{//图片画册为空，文字画册为空，返回空值
            
           return response()->json(['code'=>0,'msg'=>'查询成功','data'=>'']);
       }
        
   } 
   /**
    * 查询画册信息
    * @param  pa_id 画册的id
    */
   public function getPictureAlbumInfo(Request $request){
       
       $validator = Validator::make(
           $data  = $request->all(), [
               'pa_id' =>'required'
           ],[
               'pa_id.required' => '画册的id,不能为空!'
           ]);
       //参数错误响应
       if ($info = $validator->errors()->first()) {
            
           return response()->json(['code' => 1,'msg' => $info,'data' => '']);
       }
        
       $condition['id']      = $data['pa_id'];
       $condition['user_id'] = $this->getUserId();
        
       unset($data['pa_id']);
       
       $info = PictureEditingService::getPictureAlbumInfo($condition);
         
       if(empty($info)){
           
           return response()->json(['code' => 1,'msg' => '查询的图片画册，没有找到！','data' => '']);
       }
       $condition['pa_id']      = $data['pa_id'];
       
       unset($condition['id']);
       
       $iList = ImageLabelService::getImageLabelLists($condition);
       
       if(!empty($iList)){
           foreach ($iList as $ik=>$iv){
               if(!empty($iv['Album_imgs'])){
                   $info['label_data'][$ik]['id']         = $iv['id'];
                   $info['label_data'][$ik]['pa_id']      = $iv['pa_id'];
                   $info['label_data'][$ik]['label_name'] = $iv['label_name'];
                   $info['label_data'][$ik]['Album_imgs'] = explode(',',$iv['Album_imgs']);
               }
           }
       }else {
           $info['label_data']=[];
       }

       $info['create_time'] =date('Y-m-d H:i:s',$info['create_time']);
       $info['update_time'] =date('Y-m-d H:i:s',$info['update_time']);
       
       $this->log('图片画册信息，查询成功', $info);
       return response()->json(['code' => 0,'msg' => '查询成功','data' => $info]);
        
   }
   
   /**
    * 处理图片画册 封面、音乐、上传多图片 函数
    *
    */
   static public function handleImgsData($request){
       
       $data = [];
       
       $data['album_cover_img']  = "";   
       
       if(!empty($cover_img_files = $request->file('album_cover_img'))){
           
        $res = SELF::handlePicFormat($cover_img_files,'p','封面格式,不正确！');//判断文件格式
        
            if(!empty($res)){
                
                return $res;
            }
            
            
           if($cover_img_files->isValid()){
       
               $ac_img_path = $cover_img_files->store('huace/imgs/album-cover-img/'.date('Y').'-'.date('m').'-'.date('d'));
       
               $data['album_cover_img'] = '/uploads/'.$ac_img_path;
           }else{
       
               return [
                   'code' => 1,
                   'msg' => '画册封面，上传失败！',
                   'data' => ''
               ];
       
           }
       
       }elseif(!empty($request->input('album_cover_img'))){
          
           $data['album_cover_img'] = $request->input('album_cover_img');
       }else{
           unset($data['album_cover_img']);
       }
     
       if(!empty($music_files = $request->file('background_music'))){
           
           $res = SELF::handlePicFormat($music_files,'m','音乐格式,不正确！');//判断文件格式
           
           if(!empty($res)){
           
               return $res;
           }
           if($music_files->isValid()){
       
               $music_path = $music_files->store('huace/imgs/background-music/'.date('Y').'-'.date('m').'-'.date('d'));
       
               $data['background_music'] = '/uploads/'.$music_path;
           }else{
                
               return [
                   'code' => 1,
                   'msg' => '背景音乐，上传失败！',
                   'data' => ''
               ];

           }
       
       }elseif(!empty($request->input('background_music'))){
           
           $data['background_music'] = $request->input('background_music');
       }else{
           $data['background_music'] = "";   
       }
       
  
       return [
               'code' => 0,
               'msg' => 'success',
               'data' => $data,
               ];
   }
   
   /**
    * 单文件上传
    *
    * @param hc_img 文件名
    * @param label_id 图片画册id 
    */
   public function upload(Request $request){
   
       if(empty($request->input('label_id')))
       {
           return response()->json(['code'=>1,'msg'=>'图片标签id,不能为空！','data'=>'']);
       }
       
       $condition['user_id'] = $this->getUserId();
       $condition['id']      = $request->input('label_id');
       
       $info = ImageLabelService::getImageLabelInfo($condition);
       
       if(empty($info)){
           
           return response()->json(['code'=>1,'msg'=>'画册标签,不存在！','data'=>'']);
       }
       
       if(!empty($hc_file = $request->file('hc_img'))){
   
              $imgs_str = '';
              
              $res = SELF::handlePicFormat($hc_file, 'p', '图片格式，不正确！');
              
              if(!empty($res)){
                  
                  return response()->json($res);
              }
               if($hc_file->isValid()){
   
                      $Album_img_path = $hc_file->store('huace/imgs/Album-imgs/'.date('Y').'-'.date('m').'-'.date('d'));
                      $path = '/uploads/'.$Album_img_path;
                      
                      if(!empty($info['Album_imgs'])){
                          
                          $updData['Album_imgs'] = $info['Album_imgs'].','.$path;
                      
                      }else{
                          
                          $updData['Album_imgs'] = $path;
                      }
                      
                      ImageLabelService::updImageLabel($condition, $updData);
                      
                      return response()->json(['code'=>0,'msg'=>'上传成功','data'=>['hc_img_path'=>$path,'label_id'=>$request->input('label_id')]]);
               
               }else{
   
                      return response()->json(['code'=>1,'msg'=>'上传失败','data'=>'']);
               }
   
       }else{
           return response()->json(['code'=>1,'msg'=>'请选择图片上传','data'=>'']);
            
       }
   
   }
   
    /**
     * 处理图片和视频格式
     */
    static public function handlePicFormat($files,$flag,$msg){
 
        $pic_video_format = 'mp3,aac,wav,wma,cda,flac,m4a,mid,mka,mp2,mpa,mpc,ape,ofr,ogg,ra,wv,ac3,dts,
                            flash,mpeg,mpeg-2,mpeg-4,midi,wmv,asf,asx,rm,rmvb,mp4,3gp,mov,m4v,avi,dat,mkv,flv,vob,
                            bmp,jpg,png,tif,gif,pcx,tga,exif,fpx,svg,psd,cdr,pcd,dxf,
                            ufo,eps,ai,raw,wmf,webp,avif,apng';
        
        $pic_format       = 'bmp,jpg,png,tif,gif,pcx,tga,exif,fpx,svg,psd,cdr,pcd,dxf,ufo,eps,ai,raw,wmf,webp,avif,apng';
        
        $music_format     = 'mp3,aac,wav,wma,cda,flac,m4a,mid,mka,mp2,mpa,mpc,ape,ofr,ogg,ra,wv,ac3,dts';
        
        $ext = $files->extension();
        
        $ext = strtolower($ext);
        
        if($flag == 'm'){
        
            $pic_video_format_arr = explode(',',$music_format);
        
        }elseif($flag == 'p'){
            
            $pic_video_format_arr = explode(',',$pic_format);
            
        }else{
            
            $pic_video_format_arr = explode(',',$pic_video_format);
        }
         
        if (!in_array($ext, $pic_video_format_arr)) {
             
            return ['code' => 1,'msg' => $msg,'data' => ''];die;
        }
         
    }
    }
